package com.mewp.permissions.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mewp.permission.system.annotation.Log;
import com.mewp.permission.system.enums.BusinessType;
import com.mewp.permission.system.enums.OperatorType;
import com.mewp.permissions.common.entity.Result;
import com.mewp.permissions.model.po.SysRole;
import com.mewp.permissions.model.vo.AssginRoleVO;
import com.mewp.permissions.model.vo.SysRoleQueryVo;
import com.mewp.permissions.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author mewp
 * @version 1.0
 * @date 2023/7/28 02:18
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/role")
public class SysRoleController {
    @Resource
    private SysRoleService roleService;

    @ApiOperation("获取全部角色列表")
    @GetMapping(value = "/list")
    public Result<List<SysRole>> findAll() {
        List<SysRole> list = roleService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "获取角色")
    @GetMapping("/{id}")
    public Result<SysRole> get(@PathVariable Long id) {
        SysRole role = roleService.getById(id);
        return Result.ok(role);
    }

    @Log(title = "角色管理", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @ApiOperation(value = "新增角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PostMapping("/")
    public Result<Boolean> save(@RequestBody SysRole role) {
        boolean result = roleService.save(role);
        return result ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "修改角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @PutMapping("/")
    public Result<Boolean> updateById(@RequestBody SysRole role) {
        boolean result = roleService.updateById(role);
        return result ? Result.ok() : Result.fail();
    }

    @ApiOperation("删除角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping(value = "/{id}")
    public Result<Boolean> removeRole(@PathVariable("id") Long id) {
        boolean result = roleService.removeById(id);
        return result ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "批量删除角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("/batch")
    public Result<Boolean> batchRemove(@RequestBody List<Long> idList) {
        boolean result = roleService.removeByIds(idList);
        return result ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "获取分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "path", required = true),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "path", required = true),
    })
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("/{page}/{size}")
    public Result<Page<SysRole>> index(@PathVariable Integer page, @PathVariable Integer size,
                                       SysRoleQueryVo roleQueryVo) {
        Page<SysRole> pageModel = roleService.selectPage(page, size, roleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("/assign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = roleService.getRolesByUserId(userId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/assign")
    public Result doAssign(@Valid @RequestBody AssginRoleVO assginRoleVo) {
        boolean result = roleService.doAssign(assginRoleVo);
        return result ? Result.ok() : Result.fail();
    }
}
